using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public partial class MainMgr : SingleMono<MainMgr>
{
    public bool isSavePlayerSettingData = false;
    public bool isSaveLevelData = false;
    PlayerSettingData playerSettingData;




    
    public void InitSaveData()
    {
        var task = LoadPlayerSettingData();
        
        task.ContinueWith(t =>
        {
            if (t.IsFaulted)
            {
                Debug.LogError(t.Exception.Message);
            }
        });
        task.ContinueWith(t =>
        {
            if (t.IsCompleted)
            {
                Debug.LogError("LoadPlayerSettingData completed");
            }
        });

    }

    public async Task SavePlayerSettingData()
    {
        playerSettingData.isEffectMute = SoundManager.Instance.GetMute(SoundStyle.effect);
        playerSettingData.isMusicMute = SoundManager.Instance.GetMute(SoundStyle.backGround);
        playerSettingData.effectVolum = SoundManager.Instance.GetVolum(SoundStyle.effect);
        playerSettingData.musicVolum = SoundManager.Instance.GetVolum(SoundStyle.backGround);
        try
        {
            await SaveDataManager.Instance.SaveDataUniTask<PlayerSettingData>("PlayerSettingData", playerSettingData);
        }
        catch (Exception e) 
        {
            Debug.LogError(e.Message);
        }
    }

    public async Task LoadPlayerSettingData()
    {
        playerSettingData = await SaveDataManager.Instance.LoadDataUniTask<PlayerSettingData>("PlayerSettingData");
        if (playerSettingData == null)
        {
            playerSettingData = new PlayerSettingData();
        }
        SoundManager.Instance.SetMute(SoundStyle.effect, playerSettingData.isEffectMute);
        SoundManager.Instance.SetMute(SoundStyle.backGround, playerSettingData.isMusicMute);
        SoundManager.Instance.SetVolum(SoundStyle.effect, playerSettingData.effectVolum);
        SoundManager.Instance.SetVolum(SoundStyle.backGround, playerSettingData.musicVolum);
    }
    public async Task SaveDataUpdate()
    {
        if (isSavePlayerSettingData)
        {
           isSavePlayerSettingData = false;
           await SavePlayerSettingData();
        }
        if (isSaveLevelData)
        {
            //TODO: save level data
        }
    }

}



[Serializable]
public class PlayerSettingData
{
    public bool isEffectMute, isMusicMute;
    public float effectVolum =0.5f, musicVolum = 0.5f;

}


